﻿Imports FuncClasses

Class EditCategory

    Private _db As IDatabase
    Private _cat As Category
    Private _user As User
    Private _avm As ArticleViewModel
    Public Event LeaveEdit()
    Public Event CategoryChanged()

    Private _newparentCategory As Category
    ''' <summary>
    ''' Umbennen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Dim temp As String = ""
        If cb_renameOnArticle.IsChecked Then
            Dim tempcat As Category = _cat
            Do While tempcat IsNot Nothing
                temp = "/" + tempcat.Name + temp
                tempcat = tempcat.Parent
            Loop
        End If
        _db.DeleteCategory(_cat, _user)
        _cat.Name = tb_rename.Text
        _db.SetCategory(_cat, _user)
        If cb_renameOnArticle.IsChecked AndAlso Not String.IsNullOrEmpty(temp) Then
            Dim newcat As String = ""
            Dim tempcat As Category = _cat
            Do While tempcat IsNot Nothing
                newcat = "/" + tempcat.Name + newcat
                tempcat = tempcat.Parent
            Loop
            Dim tmplist As New List(Of Article)
            For Each item In _avm.AllArticles
                If item.Category.StartsWith(temp) Then
                    tmplist.Add(_db.GetArticle(item.ArticleID, _user))
                End If
            Next
            For Each art In tmplist
                art.ArticleCategory.Replace(temp, newcat)
                _avm.SaveArticle(art)
            Next
        End If
        RaiseEvent LeaveEdit()
        RaiseEvent CategoryChanged()
    End Sub

    ''' <summary>
    ''' Verschieben
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
        Dim temp As String = ""
        If cb_move.IsChecked Then
            Dim tempcat As Category = _cat
            Do While tempcat IsNot Nothing
                temp = "/" + tempcat.Name + temp
                tempcat = tempcat.Parent
            Loop
        End If
        _db.DeleteCategory(_cat, _user)
        _cat.Parent = _newparentCategory
        _db.SetCategory(_cat, _user)
        If cb_move.IsChecked AndAlso Not String.IsNullOrEmpty(temp) Then
            Dim newcat As String = ""
            Dim tempcat As Category = _cat
            Do While tempcat IsNot Nothing
                newcat = "/" + tempcat.Name + newcat
                tempcat = tempcat.Parent
            Loop
            Dim tmplist As New List(Of Article)
            For Each item In _avm.AllArticles
                If item.Category.StartsWith(temp) Then
                    tmplist.Add(_db.GetArticle(item.ArticleID, _user))
                End If
            Next
            For Each art In tmplist
                art.ArticleCategory.Replace(temp, newcat)
                _avm.SaveArticle(art)
            Next
        End If
        RaiseEvent LeaveEdit()
        RaiseEvent CategoryChanged()
    End Sub

    ''' <summary>
    ''' Löschen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_Click_3(sender As Object, e As RoutedEventArgs)
        Dim temp As String = ""
        If cb_remove.IsChecked Then
            Dim tempcat As Category = _cat
            Do While tempcat IsNot Nothing
                temp = "/" + tempcat.Name + temp
                tempcat = tempcat.Parent
            Loop
        End If
        _db.DeleteCategory(_cat, _user)
        _cat = _cat.Parent
        _db.SetCategory(_cat, _user)
        If cb_move.IsChecked AndAlso Not String.IsNullOrEmpty(temp) Then
            Dim newcat As String = ""
            Dim tempcat As Category = _cat
            Do While tempcat IsNot Nothing
                newcat = "/" + tempcat.Name + newcat
                tempcat = tempcat.Parent
            Loop
            Dim tmplist As New List(Of Article)
            For Each item In _avm.AllArticles
                If item.Category.StartsWith(temp) Then
                    tmplist.Add(_db.GetArticle(item.ArticleID, _user))
                End If
            Next
            For Each art In tmplist
                art.ArticleCategory.Replace(temp, newcat)
                _avm.SaveArticle(art)
            Next
        End If
        RaiseEvent LeaveEdit()
        RaiseEvent CategoryChanged()
    End Sub

    Private Sub Page_DataContextChanged_1(sender As Object, e As DependencyPropertyChangedEventArgs)
        Try
            If _cat Is Nothing Then
                _cat = DirectCast(DataContext, UICategory).CurrentCategory
                r1.Text = _cat.Name
            End If
            If DataContext Is Nothing Then
                _cat = Nothing
            End If
        Catch : End Try
    End Sub

    Public Sub SetTreeView(Treeview As TreeView)
        AddHandler Treeview.SelectedItemChanged, Sub()
                                                     Try
                                                         _newparentCategory = DirectCast(Treeview.SelectedItem, UICategory).CurrentCategory
                                                         tabcontrol1.Visibility = Windows.Visibility.Visible
                                                     Catch ex As Exception
                                                         tabcontrol1.Visibility = Windows.Visibility.Collapsed
                                                     End Try
                                                 End Sub
    End Sub

    Public Sub SetDatabase(ByRef db As IDatabase)
        _db = db
    End Sub

    Public Sub SetUser(ByRef usr As User)
        _user = usr
    End Sub

    Public Sub SetAtricleViewModel(ByRef avm As ArticleViewModel)
        _avm = avm
    End Sub

    ''' <summary>
    ''' Back
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_Click_4(sender As Object, e As RoutedEventArgs)
        RaiseEvent LeaveEdit()
    End Sub
End Class
